#include<event/epoll_evnet.h>

// 初始化epoll
bool EpollEvent::init(){
    int epoll_fd = epoll_create1(0);\
    if (epoll_fd == -1) {
        std::cout << "Failed to create epoll instance" << std::endl;
        return false;
    }
    this->event_fd = epoll_fd;
    return true;
}

bool EpollEvent::add_fd(int fd,uint32_t event,void *data){
    struct epoll_event _event{};
    _event.events = event;
    _event.data.ptr = data;
    // _event.data.fd = fd;
    if(epoll_ctl(this->event_fd, EPOLL_CTL_ADD, fd, &_event) == -1){
        return false;
    }
    return true;
}

/**void* prt 存在申请的内存时需要在销毁时释放*/
bool EpollEvent::del_fd(int fd,uint32_t event,void* data){
    struct epoll_event _event{};
    _event.events = event;
    _event.data.ptr = data;
    if(epoll_ctl(this->event_fd,EPOLL_CTL_DEL,fd,&_event) != -1){
        return false;
    }
    return true;
}

void EpollEvent::wait(){
    struct epoll_event events[10];
    while(true){
        int count = epoll_wait(this->event_fd, events, 10, -1);
        for (int i = 0; i < count; i++){
           std::cout << events[i].data.fd << std::endl;
        }
    }
}